package com.happy016.controller;

import com.happy016.pojo.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;


@RestController
public class UserRibbonConsumerController {

    private final Logger logger = LoggerFactory.getLogger(UserRibbonConsumerController.class);

    @Autowired
    private RestTemplate restTemplate;


    /**
     * hystrix 容错,出错执行 getUserFallBack
     * getUserFallBack 必须与 getUser具有相同的参数和返回值
     * @author Linyb
     * @since 2017/10/10 10:49
     */
    @HystrixCommand(fallbackMethod = "getUserFallBack")
    @GetMapping("/ribbon/consumer/user/get/{id}")
    public User getUser(@PathVariable Integer id){
        User user = restTemplate.getForObject("http://SERVICE-USER/user/get/"+id,User.class);
        return user;
    }


    /**
     * hystrix容错
     * @author Linyb
     * @since 2017/10/10 10:49
     */
    public User getUserFallBack(Integer id){
        User user = new User();
        user.setId(id);
        user.setUsername("hystric");
        return user;
    }
}
